/*
 *  JDiv - API for Multimedia Application
 *  http://jdiv.sourceforge.net 
 *  ------------------------------------
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software 
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */

package com.jdiv.physics;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.Joint;
import com.badlogic.gdx.physics.box2d.joints.MouseJoint;
import com.badlogic.gdx.physics.box2d.joints.MouseJointDef;
import com.jdiv.Const;
import com.jdiv.kernel.JDivSystem;

/** Mouse joint. This requires a world target point, tuning parameters, and the time step. */

public class JointMouse {
	
	MouseJointDef jointDef;
	Vector2 v2;
	Joint joint;
	
	/**
	 * Constructor of JointMouse.
	 * @param bodyA Body of object A.
	 * @param bodyB Body of object B.
	 * @param collideConnected Sets if they collide with each other.
	 */
	
	public JointMouse( Body bodyA, Body bodyB, boolean collideConnected ){
		
		jointDef = new MouseJointDef();
		
		v2 = new Vector2();
		
		joint = null;
		
		init( bodyA, bodyB, collideConnected );
	}
	
	/**
	 * Initialize the bodies.
	 * @param bodyA Body of object A.
	 * @param bodyB Body of object B.
	 * @param collideConnected Sets if they collide with each other.
	 */
	
	public void init ( Body bodyA, Body bodyB, boolean collideConnected ){
		
		if( joint != null ) return;
		
		jointDef.bodyA = bodyA;
		jointDef.bodyB = bodyB;
		jointDef.collideConnected = collideConnected;
	}
	
	/**
	 * @param dampingRatio The damping ratio.
	 */
	
	public void setDampingRatio( float dampingRatio ){
		jointDef.dampingRatio = dampingRatio; 
	}
	
	/**
	 * @param frequencyHz The response speed.
	 */
	
	public void setFrequencyHz( float frequencyHz ){
		jointDef.frequencyHz = frequencyHz; 
	}
	
	/**
	 * @param maxForce The maximum constraint force that can be exerted to move the candidate body.
	 */
	
	public void setMaxForce( float maxForce ){
		jointDef.maxForce = maxForce;
	}
	
	/**
	 * The initial world target point.
	 * @param x Coordinate X.
	 * @param y Coordinate Y.
	 */
	
	public void setTarget( float x, float y ){
		jointDef.target.set(x * Const.BOX2D_SCREEN_TO_PHYSICS, 
				            y * Const.BOX2D_SCREEN_TO_PHYSICS);
	}
	
	/**
	 * The world target point.
	 * @param x Coordinate X.
	 * @param y Coordinate Y.
	 */
	
	public void setTargetJoint( float x, float y ){
		if( joint == null ) return;
			
		v2.set(x * Const.BOX2D_SCREEN_TO_PHYSICS, 
			   y * Const.BOX2D_SCREEN_TO_PHYSICS);
			
		((MouseJoint)joint).setTarget(v2);
		
	}
	
	/**
	 * @return Gets MouseJoint of LibGdx.
	 */
	
	public MouseJoint getMouseJoint(){
		return (MouseJoint)joint;
	}
	
	/**
	 * Add the joint to the physical world.
	 * @return The joint in the physical world.
	 */
	
	public Joint addToWorld(){
		 if( joint != null ) return joint;
		 
		 if( JDivSystem.box2dWorld != null )	
		  joint = JDivSystem.box2dWorld.createJoint(jointDef);
		 
		 return joint;
	}
	
	/** Delete the joint in the physical world. */
	
	public void delete(){
		if( (JDivSystem.box2dWorld == null) || (joint == null) ) return;
		
		JDivSystem.box2dWorld.destroyJoint(joint);
		joint = null;
	}

}
